//
//  RACExampleTests.m
//  RACExampleTests
//
//  Created by 山刘 on 2019/8/29.
//  Copyright © 2019 山刘. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <ReactiveObjC/ReactiveObjC.h>

@interface RACExampleTests : XCTestCase

@end

@implementation RACExampleTests

-(void)testRACCommandItxueyuan{
    
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        // input 即是执行 execute:(id)input; 传进来的值   (3)
        NSLog(@"init RACCommand block 被执行 initWithSignalBlock input = %@ , thread = %@",input,[NSThread currentThread]);
        
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            // (6)
            NSLog(@"内部创建的信号block 被执行 createSignal , thread = %@",[NSThread currentThread]);
            
            // 发送信号
            [subscriber sendNext:@"create Signal for somthing %@"];
            [subscriber sendCompleted];
            
            return [RACDisposable disposableWithBlock:^{
                // 当 [subscriber sendCompleted] 调用时就会执行释放功能的 block (8)
                NSLog(@"内部信号被释放 disposableWithBlock , thread = %@",[NSThread currentThread]);
            }];
            
        }];
    }];
    
    // 订阅最新发出来信号的 signal (7)
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"执行最近的 signal , x = %@ , thread = %@",x,[NSThread currentThread]);
    }];
    
    // executionSignals 这里传的 x 值类型为 RACDynamicSignal 类型对象 (5)
    [command.executionSignals subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"executionSignals subscribeNext x = %@ , thread = %@",x,[NSThread currentThread]);
    }];
    
    // 查看将要执行，每执行完一个步聚 都会调用一次查看哪个 signal block（即 第 x 个 block  ） 将被使用 (2)(4)(9)
    // signal 的 skip: 方法功能是跳过 skipCount 个 使用 block 的查看
    [[[command executing] skip:0] subscribeNext:^(NSNumber * _Nullable x) {
        NSLog(@"executing signal subscribeNext x = %@ , thread = %@",x,[NSThread currentThread]);
    }];
    
    
    // 只执行一次 (1)
    [command execute:@"我要执行啦*****"];
}
- (RACSignal *)syncSignal
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        for (int i = 0; i < 5; i++) {
            NSLog(@"signal -- %d", i);
            [subscriber sendNext:@(i)];
        }
        [subscriber sendCompleted];
        
        return nil;
    }];
}

- (RACSignal *)asyncSignal
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        for (int i = 0; i < 5; i++) {
            NSLog(@"signal -- %d", i);
            [subscriber sendNext:@(i)];
        }
        [[RACScheduler mainThreadScheduler] afterDelay:3 schedule:^{
            NSLog(@"signal -- completed");
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}
- (RACSignal *)errorSignal1
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSLog(@"error -- le");
        [subscriber sendNext:@"errorSignal"];
        [subscriber sendError:nil];
        
        return nil;
    }];
}


- (void)setUp {
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
    }];
}

#pragma mark - RACSignal+Operations

/**
 通过interval设置一个信号值发送的延时时间。然后通过(BOOL (^)(id next))predicate作为判断哪些信号值应当被延时发送，
 如果在interval内有新的值，便会取消掉之前延时的任务，也就是把之前的值给舍去掉，
 如果在interval内信号完成所有操作，就会直接发送最后一个值并发送完成信号;
 如果新值或者完成信息在interval内没有到来，就会发送延时任务值，达到interval内发送一次的效果。也就是节流。
 
 - (RACSignal<ValueType> *)throttle:(NSTimeInterval)interval valuesPassingTest:(BOOL (^)(id _Nullable next))predicate
 */
- (void)test_throttle_valuesPassingTest {
    [[[self syncSignal] throttle:2 valuesPassingTest:^BOOL(id  _Nullable next) {
        NSLog(@"predicate:%@", next);
        return [next integerValue] == 2;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext:%@", x);
    }];
    /*
     [next integerValue] == 2 的结果：
     signal -- 0
     predicate:0
     subscribeNext:0
     
     signal -- 1
     predicate:1
     subscribeNext:1
     
     signal -- 2
     predicate:2
     
     signal -- 3
     predicate:3
     subscribeNext:3
     
     signal -- 4
     predicate:4
     subscribeNext:4
     */
    
    /*
     [next integerValue] == 4 的结果：
     signal -- 0
     predicate:0
     subscribeNext:0
     
     signal -- 1
     predicate:1
     subscribeNext:1
     
     signal -- 2
     predicate:2
     subscribeNext:2
     
     signal -- 3
     predicate:3
     subscribeNext:3
     
     signal -- 4
     predicate:4
     subscribeNext:4
     */
}

-(void)test_delay{
    
    NSLog(@"start");
    [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        for (int i = 0; i < 5; i++) {
            NSLog(@"signal -- %d", i);
            [subscriber sendNext:@(i)];
        }
        [subscriber sendCompleted];
        
        return nil;
    }] delay:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext:%@", x);
    }];
    
    // 保证上面的延时操作得以完成
    [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return nil;
    }] asynchronouslyWaitUntilCompleted:nil];
    
    /*
     2019-08-06 18:23:32.505625+0800 RACDemo02[81462:700830] start
     2019-08-06 18:23:32.508534+0800 RACDemo02[81462:700830] signal -- 0
     2019-08-06 18:23:32.508903+0800 RACDemo02[81462:700830] signal -- 1
     2019-08-06 18:23:32.509073+0800 RACDemo02[81462:700830] signal -- 2
     2019-08-06 18:23:32.509249+0800 RACDemo02[81462:700830] signal -- 3
     2019-08-06 18:23:32.509373+0800 RACDemo02[81462:700830] signal -- 4
     
     2019-08-06 18:23:34.509911+0800 RACDemo02[81462:700830] subscribeNext:0
     2019-08-06 18:23:34.510092+0800 RACDemo02[81462:700830] subscribeNext:1
     2019-08-06 18:23:34.510178+0800 RACDemo02[81462:700830] subscribeNext:2
     2019-08-06 18:23:34.510257+0800 RACDemo02[81462:700830] subscribeNext:3
     2019-08-06 18:23:34.510342+0800 RACDemo02[81462:700830] subscribeNext:4
     */
}

-(void)test_sync_repeat{
    [[[self syncSignal] repeat] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext:%@", x);
    }];
    /*
     2019-08-06 18:32:39.753477+0800 RACDemo02[82159:707650] signal -- 0
     2019-08-06 18:32:39.753642+0800 RACDemo02[82159:707650] subscribeNext:0
     2019-08-06 18:32:39.753743+0800 RACDemo02[82159:707650] signal -- 1
     2019-08-06 18:32:39.753823+0800 RACDemo02[82159:707650] subscribeNext:1
     2019-08-06 18:32:39.753901+0800 RACDemo02[82159:707650] signal -- 2
     2019-08-06 18:32:39.753977+0800 RACDemo02[82159:707650] subscribeNext:2
     2019-08-06 18:32:39.754047+0800 RACDemo02[82159:707650] signal -- 3
     2019-08-06 18:32:39.754152+0800 RACDemo02[82159:707650] subscribeNext:3
     2019-08-06 18:32:39.754423+0800 RACDemo02[82159:707650] signal -- 4
     2019-08-06 18:32:39.754665+0800 RACDemo02[82159:707650] subscribeNext:4
     2019-08-06 18:32:39.755500+0800 RACDemo02[82159:707650] signal -- 0
     2019-08-06 18:32:39.755906+0800 RACDemo02[82159:707650] subscribeNext:0
     2019-08-06 18:32:39.756212+0800 RACDemo02[82159:707650] signal -- 1
     2019-08-06 18:32:39.756418+0800 RACDemo02[82159:707650] subscribeNext:1
     2019-08-06 18:32:39.756661+0800 RACDemo02[82159:707650] signal -- 2
     2019-08-06 18:32:39.769406+0800 RACDemo02[82159:707650] subscribeNext:2
     2019-08-06 18:32:39.769650+0800 RACDemo02[82159:707650] signal -- 3
     2019-08-06 18:32:39.769751+0800 RACDemo02[82159:707650] subscribeNext:3
     2019-08-06 18:32:39.769835+0800 RACDemo02[82159:707650] signal -- 4
     2019-08-06 18:32:39.769913+0800 RACDemo02[82159:707650] subscribeNext:4
     ...
     */
}

-(void)test_async_repeat{
    [[[self asyncSignal] repeat] subscribeNext:^(id  _Nullable x) {
    }];
    // 保证上面的延时操作得以完成
    [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return nil;
    }] asynchronouslyWaitUntilCompleted:nil];
    /*
     2019-08-06 18:32:39.753477+0800 RACDemo02[82159:707650] signal -- 0
     2019-08-06 18:32:39.753642+0800 RACDemo02[82159:707650] subscribeNext:0
     2019-08-06 18:32:39.753743+0800 RACDemo02[82159:707650] signal -- 1
     2019-08-06 18:32:39.753823+0800 RACDemo02[82159:707650] subscribeNext:1
     2019-08-06 18:32:39.753901+0800 RACDemo02[82159:707650] signal -- 2
     2019-08-06 18:32:39.753977+0800 RACDemo02[82159:707650] subscribeNext:2
     2019-08-06 18:32:39.754047+0800 RACDemo02[82159:707650] signal -- 3
     2019-08-06 18:32:39.754152+0800 RACDemo02[82159:707650] subscribeNext:3
     2019-08-06 18:32:39.754423+0800 RACDemo02[82159:707650] signal -- 4
     2019-08-06 18:32:39.754665+0800 RACDemo02[82159:707650] subscribeNext:4
     2019-08-06 18:32:39.755500+0800 RACDemo02[82159:707650] signal -- 0
     2019-08-06 18:32:39.755906+0800 RACDemo02[82159:707650] subscribeNext:0
     2019-08-06 18:32:39.756212+0800 RACDemo02[82159:707650] signal -- 1
     2019-08-06 18:32:39.756418+0800 RACDemo02[82159:707650] subscribeNext:1
     2019-08-06 18:32:39.756661+0800 RACDemo02[82159:707650] signal -- 2
     2019-08-06 18:32:39.769406+0800 RACDemo02[82159:707650] subscribeNext:2
     2019-08-06 18:32:39.769650+0800 RACDemo02[82159:707650] signal -- 3
     2019-08-06 18:32:39.769751+0800 RACDemo02[82159:707650] subscribeNext:3
     2019-08-06 18:32:39.769835+0800 RACDemo02[82159:707650] signal -- 4
     2019-08-06 18:32:39.769913+0800 RACDemo02[82159:707650] subscribeNext:4
     ...
     */
}

-(void)test_initially_finally{
    [[[[self syncSignal] initially:^{
        NSLog(@"initially");
    }] finally:^{
        NSLog(@"finally");
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-06 18:40:25.863457+0800 RACDemo02[82725:712943] initially
     2019-08-06 18:40:25.863651+0800 RACDemo02[82725:712943] signal -- 0
     2019-08-06 18:40:25.863782+0800 RACDemo02[82725:712943] subscribeNext -- 0
     2019-08-06 18:40:25.863878+0800 RACDemo02[82725:712943] signal -- 1
     2019-08-06 18:40:25.863998+0800 RACDemo02[82725:712943] subscribeNext -- 1
     2019-08-06 18:40:25.864079+0800 RACDemo02[82725:712943] signal -- 2
     2019-08-06 18:40:25.864161+0800 RACDemo02[82725:712943] subscribeNext -- 2
     2019-08-06 18:40:25.864254+0800 RACDemo02[82725:712943] signal -- 3
     2019-08-06 18:40:25.864341+0800 RACDemo02[82725:712943] subscribeNext -- 3
     2019-08-06 18:40:25.864490+0800 RACDemo02[82725:712943] signal -- 4
     2019-08-06 18:40:25.864721+0800 RACDemo02[82725:712943] subscribeNext -- 4
     2019-08-06 18:40:25.865057+0800 RACDemo02[82725:712943] finally
     */
}


/**
 报错
 */
-(void)test_bufferWithTime_onScheduler{
    [[[self syncSignal] bufferWithTime:2 onScheduler:[RACScheduler immediateScheduler]] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
}

-(void)test_take{
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        for (int i = 0; i < 5; i++) {
            NSLog(@"subscriber sendNext -- %d", i);
            [subscriber sendNext:@(i)];
        }
        NSLog(@"sendCompleted");
        [subscriber sendCompleted];
        
        return nil;
    }];
//    [signal subscribeNext:^(id  _Nullable x) {
//        NSLog(@"subscribeNext -- %@", x);
//    }];
    /*
     2020-06-03 14:51:53.558366+0800 RACExample[30888:2260368] subscriber sendNext -- 0
     2020-06-03 14:51:53.558606+0800 RACExample[30888:2260368] subscribeNext -- 0
     2020-06-03 14:51:53.558714+0800 RACExample[30888:2260368] subscriber sendNext -- 1
     2020-06-03 14:51:53.558807+0800 RACExample[30888:2260368] subscribeNext -- 1
     2020-06-03 14:51:53.558898+0800 RACExample[30888:2260368] subscriber sendNext -- 2
     2020-06-03 14:51:53.558985+0800 RACExample[30888:2260368] subscribeNext -- 2
     2020-06-03 14:51:53.559069+0800 RACExample[30888:2260368] subscriber sendNext -- 3
     2020-06-03 14:51:53.559159+0800 RACExample[30888:2260368] subscribeNext -- 3
     2020-06-03 14:51:53.559249+0800 RACExample[30888:2260368] subscriber sendNext -- 4
     2020-06-03 14:51:53.559439+0800 RACExample[30888:2260368] subscribeNext -- 4
     2020-06-03 14:51:53.559708+0800 RACExample[30888:2260368] sendCompleted
     */
    [[[signal skip:1] take:1] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2020-06-03 14:59:58.615703+0800 RACExample[31246:2265921] subscriber sendNext -- 0
     2020-06-03 14:59:58.616069+0800 RACExample[31246:2265921] subscriber sendNext -- 1
     2020-06-03 14:59:58.616254+0800 RACExample[31246:2265921] subscribeNext -- 1
     2020-06-03 14:59:58.616376+0800 RACExample[31246:2265921] subscriber sendNext -- 2
     2020-06-03 14:59:58.616494+0800 RACExample[31246:2265921] subscriber sendNext -- 3
     2020-06-03 14:59:58.616608+0800 RACExample[31246:2265921] subscriber sendNext -- 4
     */
//    [[signal take:2] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"subscribeNext -- %@", x);
//    }];
    /*
     2020-06-03 14:53:40.530949+0800 RACExample[30990:2262219] subscriber sendNext -- 0
     2020-06-03 14:53:40.531286+0800 RACExample[30990:2262219] subscribeNext -- 0
     2020-06-03 14:53:40.531476+0800 RACExample[30990:2262219] subscriber sendNext -- 1
     2020-06-03 14:53:40.531610+0800 RACExample[30990:2262219] subscribeNext -- 1
     2020-06-03 14:53:40.531734+0800 RACExample[30990:2262219] subscriber sendNext -- 2
     2020-06-03 14:53:40.531829+0800 RACExample[30990:2262219] subscriber sendNext -- 3
     2020-06-03 14:53:40.531924+0800 RACExample[30990:2262219] subscriber sendNext -- 4
     2020-06-03 14:53:40.532056+0800 RACExample[30990:2262219] sendCompleted
     */
    
//    [[[self syncSignal] takeLast:1] subscribeNext:^(id  _Nullable x) {
//        NSLog(@"subscribeNext -- %@", x);
//    }];
    
    /*
     2019-08-06 18:50:49.832144+0800 RACDemo02[83425:719529] signal -- 0
     2019-08-06 18:50:49.832339+0800 RACDemo02[83425:719529] signal -- 1
     2019-08-06 18:50:49.832420+0800 RACDemo02[83425:719529] signal -- 2
     2019-08-06 18:50:49.832523+0800 RACDemo02[83425:719529] signal -- 3
     2019-08-06 18:50:49.832617+0800 RACDemo02[83425:719529] signal -- 4
     2019-08-06 18:50:49.832822+0800 RACDemo02[83425:719529] subscribeNext -- 4
     */
}

-(void)test_takeLast{
    [[[self syncSignal] takeLast:2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-06 18:50:49.832144+0800 RACDemo02[83425:719529] signal -- 0
     2019-08-06 18:50:49.832339+0800 RACDemo02[83425:719529] signal -- 1
     2019-08-06 18:50:49.832420+0800 RACDemo02[83425:719529] signal -- 2
     2019-08-06 18:50:49.832523+0800 RACDemo02[83425:719529] signal -- 3
     2019-08-06 18:50:49.832617+0800 RACDemo02[83425:719529] signal -- 4
     2019-08-06 18:50:49.832822+0800 RACDemo02[83425:719529] subscribeNext -- 4
     */
}

-(void)test_combineLatestWith{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    [[s1 combineLatestWith:s2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-06 18:56:12.407918+0800 RACDemo02[83797:722588] subscribeNext -- <RACTwoTuple: 0x6000009ffe00> (
     1,
     2
     )
     */
}

-(void)test_combineLatest{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    [[RACSignal combineLatest:@[s1,s2]] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-06 19:03:22.899605+0800 RACDemo02[84274:726455] subscribeNext -- <RACTuple: 0x600002d482f0> (
     1,
     2
     )
     */
}

-(void)test_combineLatest_reduce{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    
    [[RACSignal combineLatest:@[s1,s2] reduce:^(NSString*x,NSString*y){
        return [x stringByAppendingString:y];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-06 19:10:05.391349+0800 RACDemo02[84758:730770] subscribeNext -- 12
     */
}

-(void)test_merge{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    [[s1 merge:s2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-06 19:11:53.568706+0800 RACDemo02[84891:732066] subscribeNext -- 1
     2019-08-06 19:11:53.569084+0800 RACDemo02[84891:732066] subscribeNext -- 2
     */
    
    [[RACSignal merge:@[s1,s2]] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-06 19:11:53.568706+0800 RACDemo02[84891:732066] subscribeNext -- 1
     2019-08-06 19:11:53.569084+0800 RACDemo02[84891:732066] subscribeNext -- 2
     */
}

-(void)test_flatten{
}

-(void)test_then{
    // 在这里尽量使用 RACReplaySubject 类 ，因为 RACReplaySubject 可以先发送信号，订阅代码可以放在之后写。
    // 如果 使用 RACSignal 或 RACSubject ，那么必须要等这些对象订阅完后，发送的信号才能接收的到
    RACReplaySubject * subjectA = [RACReplaySubject subject];
    
    // 这就是好处,先发送
    [subjectA sendNext:@"AA"];
    // 必须要调用这个方法才会来到 then 后的 block
    [subjectA sendCompleted];
    
    // 按指定的顺序接收到信号
    [[[subjectA then:^RACSignal * _Nonnull{
        
        // 当 subjectA 发送信号完成后 才执行 当前的 block
        RACReplaySubject * subjectB = [RACReplaySubject subject];
        
        // 可以单独调用发送信号完成的方法就可以接着执行下一个 then
        [subjectB sendCompleted];
        
        return subjectB ;
        
    }] then:^RACSignal * _Nonnull{
        
        // 当 subjectB 发送信号完成后 才执行 当前的 block
        RACReplaySubject * subjectC = [RACReplaySubject subject];
        
        // subjectC 发送信号
        [subjectC sendNext:@"CC"];
        
        return subjectC ;
        
    }] subscribeNext:^(id  _Nullable x) { // 这个就 "相当于" 订阅了 subjectC 对象(但真正的对象则不是 subjectC 对象) ，x = @"CC"
        NSLog(@"RACReplaySubject C x = %@",x);
    }];
    
    /*
     2019-08-07 09:55:01.134694+0800 RACDemo02[44349:1110585] signal -- 0
     2019-08-07 09:55:01.134964+0800 RACDemo02[44349:1110585] signal -- 1
     2019-08-07 09:55:01.135102+0800 RACDemo02[44349:1110585] signal -- 2
     2019-08-07 09:55:01.135206+0800 RACDemo02[44349:1110585] signal -- 3
     2019-08-07 09:55:01.135304+0800 RACDemo02[44349:1110585] signal -- 4
     2019-08-07 09:55:01.135427+0800 RACDemo02[44349:1110585] then
     2019-08-07 09:55:01.135512+0800 RACDemo02[44349:1110585] subscribeNext -- 1
     */
}

-(void)test_aggregateWithStart_reduce{
    [[[self syncSignal] aggregateWithStart:@100 reduce:^id _Nonnull(NSNumber*  _Nonnull running, NSNumber*   _Nonnull next) {
        NSLog(@"running:%d,next:%d",[running intValue], [next intValue]);
        return @([running intValue] + [next intValue]);
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-07 09:53:25.665604+0800 RACDemo02[44206:1109314] signal -- 0
     2019-08-07 09:53:25.665773+0800 RACDemo02[44206:1109314] running:100,next:0
     2019-08-07 09:53:25.665932+0800 RACDemo02[44206:1109314] signal -- 1
     2019-08-07 09:53:25.666020+0800 RACDemo02[44206:1109314] running:100,next:1
     2019-08-07 09:53:25.666152+0800 RACDemo02[44206:1109314] signal -- 2
     2019-08-07 09:53:25.666239+0800 RACDemo02[44206:1109314] running:101,next:2
     2019-08-07 09:53:25.666341+0800 RACDemo02[44206:1109314] signal -- 3
     2019-08-07 09:53:25.666422+0800 RACDemo02[44206:1109314] running:103,next:3
     2019-08-07 09:53:25.666521+0800 RACDemo02[44206:1109314] signal -- 4
     2019-08-07 09:53:25.666619+0800 RACDemo02[44206:1109314] running:106,next:4
     2019-08-07 09:53:25.666757+0800 RACDemo02[44206:1109314] subscribeNext -- 110
     */
}

-(void)test_aggregateWithStart_reduceWithIndex{
    [[[self syncSignal] aggregateWithStart:@100 reduceWithIndex:^id _Nonnull(NSNumber*  _Nonnull running, NSNumber*  _Nonnull next, NSUInteger index) {
        NSLog(@"running:%d,next:%d,index:%ld",[running intValue], [next intValue],index);
        return @([running intValue] + [next intValue]);
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-07 09:57:05.249109+0800 RACDemo02[44548:1112397] signal -- 0
     2019-08-07 09:57:05.249304+0800 RACDemo02[44548:1112397] running:100,next:0,index:0
     2019-08-07 09:57:05.249457+0800 RACDemo02[44548:1112397] signal -- 1
     2019-08-07 09:57:05.249546+0800 RACDemo02[44548:1112397] running:100,next:1,index:1
     2019-08-07 09:57:05.249648+0800 RACDemo02[44548:1112397] signal -- 2
     2019-08-07 09:57:05.249729+0800 RACDemo02[44548:1112397] running:101,next:2,index:2
     2019-08-07 09:57:05.249840+0800 RACDemo02[44548:1112397] signal -- 3
     2019-08-07 09:57:05.250081+0800 RACDemo02[44548:1112397] running:103,next:3,index:3
     2019-08-07 09:57:05.250949+0800 RACDemo02[44548:1112397] signal -- 4
     2019-08-07 09:57:05.251621+0800 RACDemo02[44548:1112397] running:106,next:4,index:4
     2019-08-07 09:57:05.252298+0800 RACDemo02[44548:1112397] subscribeNext -- 110
     */
}

-(void)test_aggregateWithStartFactory_reduce{
    [[[self syncSignal] aggregateWithStartFactory:^id _Nonnull{
        return @100;
    } reduce:^id _Nonnull(NSNumber*  _Nonnull running, NSNumber*  _Nonnull next) {
        NSLog(@"running:%d,next:%d",[running intValue], [next intValue]);
        return @([running intValue] + [next intValue]);
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-07 09:59:45.531688+0800 RACDemo02[44766:1114499] signal -- 0
     2019-08-07 09:59:45.531916+0800 RACDemo02[44766:1114499] running:100,next:0
     2019-08-07 09:59:45.532140+0800 RACDemo02[44766:1114499] signal -- 1
     2019-08-07 09:59:45.532266+0800 RACDemo02[44766:1114499] running:100,next:1
     2019-08-07 09:59:45.532405+0800 RACDemo02[44766:1114499] signal -- 2
     2019-08-07 09:59:45.532500+0800 RACDemo02[44766:1114499] running:101,next:2
     2019-08-07 09:59:45.532619+0800 RACDemo02[44766:1114499] signal -- 3
     2019-08-07 09:59:45.532731+0800 RACDemo02[44766:1114499] running:103,next:3
     2019-08-07 09:59:45.533120+0800 RACDemo02[44766:1114499] signal -- 4
     2019-08-07 09:59:45.533429+0800 RACDemo02[44766:1114499] running:106,next:4
     2019-08-07 09:59:45.534021+0800 RACDemo02[44766:1114499] subscribeNext -- 110
     */
}
-(void)test_interval_onScheduler{
    [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"interval -- %@", x);
    }];
    // 保证上面的延时操作得以完成
    [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return nil;
    }] asynchronouslyWaitUntilCompleted:nil];
    
    /*
     2019-08-07 10:10:38.795691+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:38 2019
     2019-08-07 10:10:39.796486+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:39 2019
     2019-08-07 10:10:40.796486+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:40 2019
     2019-08-07 10:10:41.796472+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:41 2019
     2019-08-07 10:10:42.796452+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:42 2019
     2019-08-07 10:10:43.796631+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:43 2019
     2019-08-07 10:10:44.796494+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:44 2019
     2019-08-07 10:10:45.796493+0800 RACDemo02[45596:1122009] interval -- Wed Aug  7 10:10:45 2019
     */
}
-(void)test_interval_onScheduler_withLeeway{
    [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler] withLeeway:2] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"interval -- %@", x);
    }];
    // 保证上面的延时操作得以完成
    [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return nil;
    }] asynchronouslyWaitUntilCompleted:nil];
    
    /*
     2019-08-07 10:14:23.637393+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:23 2019
     2019-08-07 10:14:24.647823+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:24 2019
     2019-08-07 10:14:25.637348+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:25 2019
     2019-08-07 10:14:26.637380+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:26 2019
     2019-08-07 10:14:27.648556+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:27 2019
     2019-08-07 10:14:28.658269+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:28 2019
     2019-08-07 10:14:29.668395+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:29 2019
     2019-08-07 10:14:30.678426+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:30 2019
     2019-08-07 10:14:31.688287+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:31 2019
     2019-08-07 10:14:32.695847+0800 RACDemo02[45898:1124954] interval -- Wed Aug  7 10:14:32 2019
     */
}

-(void)test_takeUntil{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    //原始信号一直发送信号，直到，替代的信号发出事件，原始信号终止。
    [[s1 takeUntil:s2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    // 保证上面的延时操作得以完成
    [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        return nil;
    }] asynchronouslyWaitUntilCompleted:nil];
}

-(void)test_takeUntilReplacement{
    RACSignal *s1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"1"];
        return nil;
    }];
    RACSignal *s2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"2"];
        return nil;
    }];
    //原始信号一直发送信号，直到，替代的信号发出事件，原始信号终止。
    [[s1 takeUntilReplacement:s2] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
}
-(void)test_catch{
    RACSignal *(^catchBlock)(NSError *error) = ^(NSError *error){
        NSLog(@"catchBlock");
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSLog(@"catchSignal");
            [subscriber sendNext:@"catchSignal"];
            return nil;
        }];
    };
    [[[self errorSignal1] catch:catchBlock] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-07 10:40:15.306262+0800 RACDemo02[47946:1143322] error -- le
     2019-08-07 10:40:15.306452+0800 RACDemo02[47946:1143322] subscribeNext -- errorSignal
     2019-08-07 10:40:15.306600+0800 RACDemo02[47946:1143322] catchBlock
     2019-08-07 10:40:15.306710+0800 RACDemo02[47946:1143322] catchSignal
     2019-08-07 10:40:15.306796+0800 RACDemo02[47946:1143322] subscribeNext -- catchSignal
     */
}
-(void)test_catchTo{
    RACSignal *catchSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"catchSignal");
        [subscriber sendNext:@"catchSignal"];
        return nil;
    }];
    
    [[[self errorSignal1] catchTo:catchSignal] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-07 10:42:17.094178+0800 RACDemo02[48106:1144648] error -- le
     2019-08-07 10:42:17.094484+0800 RACDemo02[48106:1144648] subscribeNext -- errorSignal
     2019-08-07 10:42:17.094690+0800 RACDemo02[48106:1144648] catchSignal
     2019-08-07 10:42:17.094796+0800 RACDemo02[48106:1144648] subscribeNext -- catchSignal
     */
}
-(void)test_tryMap{
    [[[self syncSignal] tryMap:^id _Nonnull(NSNumber*  _Nullable value, NSError * _Nullable __autoreleasing * _Nullable errorPtr) {
        if ([value intValue] == 0) {
            return @(100);
        }
        return nil;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    /*
     2019-08-12 16:01:30.462975+0800 RACDemo02[42455:233967] signal -- 0
     2019-08-12 16:01:30.463273+0800 RACDemo02[42455:233967] subscribeNext -- 100
     2019-08-12 16:01:30.463463+0800 RACDemo02[42455:233967] signal -- 1
     2019-08-12 16:01:30.463604+0800 RACDemo02[42455:233967] signal -- 2
     2019-08-12 16:01:30.463696+0800 RACDemo02[42455:233967] signal -- 3
     2019-08-12 16:01:30.463787+0800 RACDemo02[42455:233967] signal -- 4
     */
}

#pragma mark - RACSignal+RACStreamOperations

-(void)test_scanWithStart_reduce{
    [[[self syncSignal] scanWithStart:@1 reduce:^id _Nullable(NSNumber*  _Nullable running, NSNumber*  _Nullable next) {
        NSLog(@"running：%@，next：%@", running,next);
        return nil;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*nil
     2019-08-12 17:43:33.034663+0800 RACDemo02[49824:298459] signal -- 0
     2019-08-12 17:43:33.034920+0800 RACDemo02[49824:298459] running：1，next：0
     2019-08-12 17:43:33.035368+0800 RACDemo02[49824:298459] subscribeNext -- 0
     
     2019-08-12 17:43:33.036121+0800 RACDemo02[49824:298459] signal -- 1
     2019-08-12 17:43:33.036694+0800 RACDemo02[49824:298459] running：0，next：1
     2019-08-12 17:43:33.037244+0800 RACDemo02[49824:298459] subscribeNext -- 1
     
     2019-08-12 17:43:33.037670+0800 RACDemo02[49824:298459] signal -- 2
     2019-08-12 17:43:33.037843+0800 RACDemo02[49824:298459] running：1，next：2
     2019-08-12 17:43:33.037956+0800 RACDemo02[49824:298459] subscribeNext -- 2
     
     2019-08-12 17:43:33.038056+0800 RACDemo02[49824:298459] signal -- 3
     2019-08-12 17:43:33.038158+0800 RACDemo02[49824:298459] running：2，next：3
     2019-08-12 17:43:33.038261+0800 RACDemo02[49824:298459] subscribeNext -- 3
     
     2019-08-12 17:43:33.038345+0800 RACDemo02[49824:298459] signal -- 4
     2019-08-12 17:43:33.038609+0800 RACDemo02[49824:298459] running：3，next：4
     2019-08-12 17:43:33.038914+0800 RACDemo02[49824:298459] subscribeNext -- 4
     */
    
}
-(void)test_scanWithStart_reduceWithIndex{
    [[[self syncSignal] scanWithStart:@100 reduceWithIndex:^id _Nullable(NSNumber*  _Nullable running, NSNumber*  _Nullable next, NSUInteger index) {
        NSLog(@"running：%@，next：%@，index：%zd", running,next,index);
        return nil;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    
    /*
     2019-08-12 17:57:56.979065+0800 RACDemo02[50934:309020] signal -- 0
     2019-08-12 17:57:56.979259+0800 RACDemo02[50934:309020] running：1，next：0，index：0
     2019-08-12 17:57:56.979454+0800 RACDemo02[50934:309020] subscribeNext -- 0
     2019-08-12 17:57:56.979615+0800 RACDemo02[50934:309020] signal -- 1
     2019-08-12 17:57:56.979733+0800 RACDemo02[50934:309020] running：0，next：1，index：1
     2019-08-12 17:57:56.979843+0800 RACDemo02[50934:309020] subscribeNext -- 1
     2019-08-12 17:57:56.979946+0800 RACDemo02[50934:309020] signal -- 2
     2019-08-12 17:57:56.980103+0800 RACDemo02[50934:309020] running：1，next：2，index：2
     2019-08-12 17:57:56.980445+0800 RACDemo02[50934:309020] subscribeNext -- 2
     2019-08-12 17:57:56.980858+0800 RACDemo02[50934:309020] signal -- 3
     2019-08-12 17:57:56.981319+0800 RACDemo02[50934:309020] running：2，next：3，index：3
     2019-08-12 17:57:56.981503+0800 RACDemo02[50934:309020] subscribeNext -- 3
     2019-08-12 17:57:56.981834+0800 RACDemo02[50934:309020] signal -- 4
     2019-08-12 17:57:56.982277+0800 RACDemo02[50934:309020] running：3，next：4，index：4
     2019-08-12 17:57:56.982442+0800 RACDemo02[50934:309020] subscribeNext -- 4
     */
}
-(void)test_map{
    RACSignal *syncSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        for (int i = 0; i < 5; i++) {
            NSLog(@"signal -- %d", i);
            [subscriber sendNext:@(i)];
        }
        [subscriber sendCompleted];
        
        return nil;
    }];
    
    [[syncSignal map:^id _Nullable(id  _Nullable value) {
        NSLog(@"map -- %@", value);
        return [NSString stringWithFormat:@"MAP+%@",value];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
    //    2019-08-30 14:00:05.098586+0800 RACExample[99686:2868124] signal -- 0
    //    2019-08-30 14:00:05.098791+0800 RACExample[99686:2868124] map -- 0
    //    2019-08-30 14:00:05.098987+0800 RACExample[99686:2868124] subscribeNext -- MAP+0
    //    2019-08-30 14:00:05.099149+0800 RACExample[99686:2868124] signal -- 1
    //    2019-08-30 14:00:05.099244+0800 RACExample[99686:2868124] map -- 1
    //    2019-08-30 14:00:05.099370+0800 RACExample[99686:2868124] subscribeNext -- MAP+1
    //    2019-08-30 14:00:05.099522+0800 RACExample[99686:2868124] signal -- 2
    //    2019-08-30 14:00:05.099601+0800 RACExample[99686:2868124] map -- 2
    //    2019-08-30 14:00:05.099717+0800 RACExample[99686:2868124] subscribeNext -- MAP+2
    //    2019-08-30 14:00:05.099866+0800 RACExample[99686:2868124] signal -- 3
    //    2019-08-30 14:00:05.100081+0800 RACExample[99686:2868124] map -- 3
    //    2019-08-30 14:00:05.100398+0800 RACExample[99686:2868124] subscribeNext -- MAP+3
    //    2019-08-30 14:00:05.100599+0800 RACExample[99686:2868124] signal -- 4
    //    2019-08-30 14:00:05.100759+0800 RACExample[99686:2868124] map -- 4
    //    2019-08-30 14:00:05.100999+0800 RACExample[99686:2868124] subscribeNext -- MAP+4
}

-(void)test_mapReplace{
    [[[self syncSignal] mapReplace:@"dd"] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
//    2019-08-30 14:34:50.749755+0800 RACExample[2360:2890432] signal -- 0
//    2019-08-30 14:34:50.750063+0800 RACExample[2360:2890432] subscribeNext -- dd
//    2019-08-30 14:34:50.750499+0800 RACExample[2360:2890432] signal -- 1
//    2019-08-30 14:34:50.750689+0800 RACExample[2360:2890432] subscribeNext -- dd
//    2019-08-30 14:34:50.750805+0800 RACExample[2360:2890432] signal -- 2
//    2019-08-30 14:34:50.750932+0800 RACExample[2360:2890432] subscribeNext -- dd
//    2019-08-30 14:34:50.751035+0800 RACExample[2360:2890432] signal -- 3
//    2019-08-30 14:34:50.751138+0800 RACExample[2360:2890432] subscribeNext -- dd
//    2019-08-30 14:34:50.751233+0800 RACExample[2360:2890432] signal -- 4
//    2019-08-30 14:34:50.751335+0800 RACExample[2360:2890432] subscribeNext -- dd
}
-(void)test_flattenMap{
    
     [[[self syncSignal] flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
     NSLog(@"flattenMap -- %@", value);
     return [[RACSignal return:value] logCompleted];
     }] subscribeNext:^(id  _Nullable x) {
     NSLog(@"subscribeNext -- %@", x);
     }];
//     2019-08-30 16:34:17.021506+0800 RACExample[14900:2980777] signal -- 0
//     2019-08-30 16:34:17.021731+0800 RACExample[14900:2980777] flattenMap -- 0
//     2019-08-30 16:34:17.021933+0800 RACExample[14900:2980777] subscribeNext -- 0
//     2019-08-30 16:34:17.022124+0800 RACExample[14900:2980777] <RACReturnSignal: 0x600003eb7ca0> name:  completed
//     2019-08-30 16:34:17.022259+0800 RACExample[14900:2980777] signal -- 1
//     2019-08-30 16:34:17.022344+0800 RACExample[14900:2980777] flattenMap -- 1
//     2019-08-30 16:34:17.022465+0800 RACExample[14900:2980777] subscribeNext -- 1
//     2019-08-30 16:34:17.022629+0800 RACExample[14900:2980777] <RACReturnSignal: 0x600003eb7ce0> name:  completed
//     2019-08-30 16:34:17.022987+0800 RACExample[14900:2980777] signal -- 2
//     2019-08-30 16:34:17.023193+0800 RACExample[14900:2980777] flattenMap -- 2
//     2019-08-30 16:34:17.023443+0800 RACExample[14900:2980777] subscribeNext -- 2
//     2019-08-30 16:34:17.023638+0800 RACExample[14900:2980777] <RACReturnSignal: 0x600003eeaf80> name:  completed
//     2019-08-30 16:34:17.023840+0800 RACExample[14900:2980777] signal -- 3
//     2019-08-30 16:34:17.024070+0800 RACExample[14900:2980777] flattenMap -- 3
//     2019-08-30 16:34:17.024311+0800 RACExample[14900:2980777] subscribeNext -- 3
//     2019-08-30 16:34:17.024599+0800 RACExample[14900:2980777] <RACReturnSignal: 0x600003eea600> name:  completed
//     2019-08-30 16:34:17.024830+0800 RACExample[14900:2980777] signal -- 4
//     2019-08-30 16:34:17.050355+0800 RACExample[14900:2980777] flattenMap -- 4
//     2019-08-30 16:34:17.050798+0800 RACExample[14900:2980777] subscribeNext -- 4
//     2019-08-30 16:34:17.050934+0800 RACExample[14900:2980777] <RACReturnSignal: 0x600003ebb0e0> name:  completed
    
    
    
    
    /*[[[[self syncSignal] flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
     NSLog(@"flattenMap -- %@", value);
     return [RACSignal return:value];
     }] logCompleted] subscribeNext:^(id  _Nullable x) {
     NSLog(@"subscribeNext -- %@", x);
     }];
     2019-08-30 16:31:12.611409+0800 RACExample[14663:2978039] signal -- 0
     2019-08-30 16:31:12.611636+0800 RACExample[14663:2978039] flattenMap -- 0
     2019-08-30 16:31:12.611833+0800 RACExample[14663:2978039] subscribeNext -- 0
     2019-08-30 16:31:12.611975+0800 RACExample[14663:2978039] signal -- 1
     2019-08-30 16:31:12.612057+0800 RACExample[14663:2978039] flattenMap -- 1
     2019-08-30 16:31:12.612175+0800 RACExample[14663:2978039] subscribeNext -- 1
     2019-08-30 16:31:12.612271+0800 RACExample[14663:2978039] signal -- 2
     2019-08-30 16:31:12.612371+0800 RACExample[14663:2978039] flattenMap -- 2
     2019-08-30 16:31:12.612483+0800 RACExample[14663:2978039] subscribeNext -- 2
     2019-08-30 16:31:12.612582+0800 RACExample[14663:2978039] signal -- 3
     2019-08-30 16:31:12.612668+0800 RACExample[14663:2978039] flattenMap -- 3
     2019-08-30 16:31:12.612766+0800 RACExample[14663:2978039] subscribeNext -- 3
     2019-08-30 16:31:12.612859+0800 RACExample[14663:2978039] signal -- 4
     2019-08-30 16:31:12.612946+0800 RACExample[14663:2978039] flattenMap -- 4
     2019-08-30 16:31:12.613038+0800 RACExample[14663:2978039] subscribeNext -- 4
     2019-08-30 16:31:12.621597+0800 RACExample[14663:2978039] <RACDynamicSignal: 0x600000c37700> name:  completed
     */
    
    

    /*
     [[[self syncSignal] flattenMap:^__kindof RACSignal * _Nullable(id  _Nullable value) {
     return nil;//[RACSignal empty]
     }] subscribeNext:^(id  _Nullable x) {
     NSLog(@"subscribeNext -- %@", x);
     }];
     signal -- 0
     signal -- 1
     signal -- 2
     signal -- 3
     signal -- 4
     */
}
-(void)test_filter{
    [[[self syncSignal] filter:^BOOL(NSNumber*  _Nullable value) {
        if (value.integerValue%2 == 0) {
            return YES;
        }
        return NO;
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext -- %@", x);
    }];
//    2019-08-30 17:03:02.077431+0800 RACExample[18379:3006837] signal -- 0
//    2019-08-30 17:03:02.077737+0800 RACExample[18379:3006837] subscribeNext -- 0
//    2019-08-30 17:03:02.077895+0800 RACExample[18379:3006837] signal -- 1
//    2019-08-30 17:03:02.078288+0800 RACExample[18379:3006837] signal -- 2
//    2019-08-30 17:03:02.078425+0800 RACExample[18379:3006837] subscribeNext -- 2
//    2019-08-30 17:03:02.078526+0800 RACExample[18379:3006837] signal -- 3
//    2019-08-30 17:03:02.078637+0800 RACExample[18379:3006837] signal -- 4
//    2019-08-30 17:03:02.078739+0800 RACExample[18379:3006837] subscribeNext -- 4
}
-(void)test_sig_1{
    //Create a Signal.
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        for (NSInteger i = 0; i <= 5; i++) {
            [subscriber sendNext:@(i)];
        }
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"RACDisposable block");
        }];
    }];
    // Subscribe a Signal
    // English: disposable: adj.可任意处理的；可自由使用的；用完即可丢弃的
    RACDisposable * disposable = [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"Recive signal:%@",x);
    } error:^(NSError * _Nullable error) {
        NSLog(@"Recive signal:%@",error.localizedDescription);
    } completed:^{
        NSLog(@"completed!");
    }] ;
    
    // Release the Subscriber
    [disposable dispose];
}
-(void)test_RACSubject_1{
    // Create a Signal
    RACSubject *subject = [RACSubject subject];
    // Subscribe the Signal,内部创建RACSubscriber
    RACDisposable * disposable1 = [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"第一个订阅者--%@", x);
    }];
    // Subscribe the Signal
    RACDisposable * disposable2 = [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"第二个订阅者---%@", x);
    }];
    //Send a Signal message
    [subject sendNext:@(1)];
    //Release the Subscriber
    [disposable1 dispose];
    [disposable2 dispose];
    
    /*
     2020-05-28 17:22:39.471032+0800 RACExample[51069:11749773] 第一个订阅者--1
     2020-05-28 17:22:39.471216+0800 RACExample[51069:11749773] 第二个订阅者---1
     */
}
-(void)test_RACReplaySubject_1{
    // Create a Signal
    RACReplaySubject *replySub = [RACReplaySubject subject];
    //Send a Signal message
    [replySub sendNext:@1];
    [replySub sendNext:@2];
    // Subscribe the Signal,内部创建RACSubscriber
    RACDisposable * disposable1 = [replySub subscribeNext:^(id  _Nullable x) {
        NSLog(@"第一个订阅者--%@", x);
    }];
    // Subscribe the Signal
    RACDisposable * disposable2 = [replySub subscribeNext:^(id  _Nullable x) {
        NSLog(@"第二个订阅者---%@", x);
    }];
    //Release the Subscriber
    [disposable1 dispose];
    [disposable2 dispose];
}
-(void)test_concat{
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@1];
        [subscriber sendCompleted];
        return nil;
    }];
    
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@2];
        return nil;
    }];
    // 把signalA拼接到signalB后，signalA发送完成，signalB才会被激活
    // 注意：第一个信号必须发送完成，第二个信号才会被激活
    [[signalA concat:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}
-(void)test_zipWith{
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@1];
        [subscriber sendCompleted];
        return nil;
    }];
    
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@2];
        return nil;
    }];
    [[signalA zipWith:signalB] subscribeNext:^(id  _Nullable x) {
        // x 的类型为 RACTuple -> RACOneTuple、RACTwoTuple、RACThreeTuple、RACFourTuple
         NSLog(@"%@",x);
    }];
}

-(void)test_combineLatest_1{
    RACReplaySubject * subjectA = [RACReplaySubject subject];
    RACReplaySubject * subjectB = [RACReplaySubject subject];
    RACReplaySubject * subjectC = [RACReplaySubject subject];
    
    // 三个对象同时发送信号，缺一不可
    [subjectA sendNext:@1];
    [subjectB sendNext:@2];
    [subjectC sendNext:@3];

    [[RACSignal combineLatest:@[subjectA,subjectB,subjectC]] subscribeNext:^(id  _Nullable x) {
        // x 的类型为 RACTuple 元组类 -> RACOneTuple、RACTwoTuple、RACThreeTuple、RACFourTuple
         NSLog(@"%@",x);
    }];
    /*
     2020-05-28 18:54:23.910080+0800 RACExample[54517:11813771] <RACTuple: 0x600002aa1410> (
         1,
         2,
         3
     )
     */
}
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
//-(void)test_
@end
